package com.zadtek.sharkRider.activity.adapter;

import java.util.List;

import android.content.Context;
import android.content.SharedPreferences;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.zadtek.sharkRider.R;
import com.zadtek.sharkRider.activity.common.Common;
import com.zadtek.sharkRider.activity.common.Const;
import com.zadtek.sharkRider.activity.data.OrderNewData;
import com.zadtek.sharkRider.views.OnChanged;

public class OrderNewAdapter extends CommonAdapter<OrderNewData> {
	private OnChanged listener;

	public OrderNewAdapter(Context context, List<OrderNewData> datas,
			int layoutId) {
		super(context, datas, layoutId);
		// TODO Auto-generated constructor stub
	}

	@Override
	public void convert(final ViewHolders holder, final OrderNewData t) {
		View view =holder.getmConvertView();
		TextView newtaskmomey = holder.getView(R.id.newtask_money);
		TextView newtasktime = holder.getView(R.id.newtask_time);
		TextView newtask_goods_getaddress = holder
				.getView(R.id.newtask_goods_getaddress);
		TextView getgpsaddr = holder.getView(R.id.getgpsaddr);
		newtaskmomey.setText("￥" + t.getReturnpay());
		if (!t.getAdd_time().equals("") && t.getAdd_time() != null && !t.getAdd_time().equals("null")) {
			newtasktime.setText(Common.getStrTime(t.getAdd_time()));
		}
		newtask_goods_getaddress.setText(t.getSendaddress());
		try {
			String address_road =t.getAddress_road();
			if (address_road ==null || address_road.equals("") || address_road.equals("null")) {
				
			String addrss = t.getGps_addrs();
			if (addrss != null && !addrss.equals("")) {
				getgpsaddr.setText(addrss); // 根据经纬度反查地址
				getgpsaddr.setVisibility(View.VISIBLE);
			} else {
				getgpsaddr.setVisibility(View.GONE);
			}
			}else{
				getgpsaddr.setText(address_road); // 根据经纬度反查地址
				getgpsaddr.setVisibility(View.VISIBLE);
			}
		} catch (Exception e) {
		}
		
		ListView ItemList = holder.getView(R.id.newtask_itemlist);
		Button submit = holder.getView(R.id.submit);
		LinearLayout show_layout1 =holder.getView(R.id.show_layout1);
		if (t.getTime_limit().equals("0")) {
			submit.setText("抢单");
		}else{
			submit.setText("抢单(还剩"+t.getTime_limit()+"秒)");
		}
		
		
		OrderNewItemAdapter itemadapter = new OrderNewItemAdapter(mContext,
				t.getOrderItemList(), R.layout.newtasklist_itemlist_item);
		ItemList.setAdapter(itemadapter);

		submit.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				SharedPreferences sp = mContext.getSharedPreferences(
						Const.PREF_NAME, Context.MODE_PRIVATE);
				String Uid = sp.getString("uid", "");
				if (null != Uid && !Uid.equals("")) {
					listener.getChoiceData(t.getMainid(), Uid,holder.getPositon());
				}
			}
		});
	}

	public void setOnChanged(OnChanged listener) {
		this.listener = listener;
	}
	
}
